﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Leo.Entities;


namespace Leo.Repository
{
    public class RolRepository : MasterRepository, IRolRepository
    {
        public List<Rol> GetAllFromRoles()
        {
            var query = from r in context.Rols
                        select r;
            return query.ToList();
        }

        public List<Rol> GetAllFromRolesByCriterio(string criterio)
        {
            var query = from r in context.Rols
                        select r;
            if (criterio!=null && criterio!=string.Empty)
            {
                query = from r in query
                        where r.Nombre.ToUpper().Contains(criterio.ToUpper()) ||
                        
                        r.Descripcion.ToUpper().Contains(criterio.ToUpper())
                        select r;

            }
            return query.ToList();
        }

        public Rol GetFromRolesByID(int id)
        {
            return context.Rols.Find(id);
        }

        public void AddRol(Rol rol)
        {
            context.Rols.Add(rol);
            context.SaveChanges();
        }

        public void UpdateRol(Rol rol)
        {
            context.Entry(rol).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveRol(int id)
        {
            var existe = context.Rols.Find(id);
            if (existe!=null)
            {
                context.Rols.Remove(existe);
                context.SaveChanges();
            }
        }
    }
}
